package com.exartisan.thanhdoanhn;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

import com.exartisan.adapter.GalleryAdapter;
import com.exartisan.bean.GalleryContent;
import com.exartisan.network.NetworkUtility;
import com.exartisan.network.RestConnector;
import com.exartisan.network.StupidResponseTranslater;
import com.exartisan.network.WeirdParametersBuilder;
import com.loopj.android.http.AsyncHttpResponseHandler;

public class GalleryActivity extends BaseActivity{

	private GalleryAdapter mAdapter;
	private GridView gvGallery;
	private Button btnPhoto;
	private Button btnVideo;
	private static boolean isPhotoSegment = true;
	private ArrayList<GalleryContent> galleryContents = new ArrayList<GalleryContent>();
	private ArrayList<GalleryContent> photoContents = new ArrayList<GalleryContent>();
	private ArrayList<GalleryContent> videoContents = new ArrayList<GalleryContent>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	
	}
	
	private void doNetWorkTask(){
		if(NetworkUtility.checkNetworkState(getApplicationContext())){
			RestConnector.post("",WeirdParametersBuilder.BuildParams(WeirdParametersBuilder.BuildDetailGalleryData(1, 1)), new AsyncHttpResponseHandler(){
				@Override
				public void onSuccess(int arg0, String response) {
					galleryContents = StupidResponseTranslater.getDetailGallery(response);
					
					photoContents = new ArrayList<GalleryContent>();
					videoContents = new ArrayList<GalleryContent>();
					for (int i = 0; i < galleryContents.size(); i++) {
						if(galleryContents.get(i).getType()==1){
							photoContents.add(galleryContents.get(i));
						} else videoContents.add(galleryContents.get(i));
					}
					mAdapter = new GalleryAdapter(getApplicationContext(), photoContents, isPhotoSegment);
					gvGallery.setAdapter(mAdapter);
					super.onSuccess(arg0, response);
				}
				
				@Override
				public void onFailure(Throwable arg0, String arg1) {
					super.onFailure(arg0, arg1);
				}
			});
		}
	}
	
	@Override
	protected void setUpActionBar() {
		super.setUpActionBar();
		getSupportActionBar().setDisplayShowHomeEnabled(false);
		getSupportActionBar().setDisplayShowTitleEnabled(false);
		getSupportActionBar().setDisplayShowCustomEnabled(true);
		getSupportActionBar().setCustomView(R.layout.ab_list);
	}

	@Override
	protected void initUI() {
	setContentView(R.layout.activity_gallery);
		
		btnPhoto = (Button) findViewById(R.id.btnPhoto);
		btnVideo = (Button) findViewById(R.id.btnVideo);
		
		gvGallery = (GridView) findViewById(R.id.gv_gallery);
		TextView tvTitle = (TextView)findViewById(R.id.tvTitle);
		tvTitle.setText("Gallery");
		
//		final ArrayList<GalleryContent> galleryContents = new ArrayList<GalleryContent>();
//		for (int i = 0; i < 20; i++) {
//			GalleryContent content = new GalleryContent(1, "http://cungthieunhi.org.vn/uploads/gallerys/DSC_0062.JPG", "abc", "def", 1);
//			galleryContents.add(content);
//		}
//		final ArrayList<GalleryContent> photoContents = new ArrayList<GalleryContent>();
//		final ArrayList<GalleryContent> videoContents = new ArrayList<GalleryContent>();
//		for (int i = 0; i < galleryContents.size(); i++) {
//			if(galleryContents.get(i).getType()==1){
//				photoContents.add(galleryContents.get(i));
//			} else videoContents.add(galleryContents.get(i));
//		}
//		videoContents.add(new GalleryContent(1, "android.resource://" + getPackageName()
//				+ "/" + R.raw.big_buck_bunny, "abc", "def", 2));
		
//		mAdapter = new GalleryAdapter(this, photoContents, isPhotoSegment);
//		gvGallery.setAdapter(mAdapter);
		
		doNetWorkTask();
		
//		photoContents.add(new GalleryContent(1, "http://www.thanhdoanhanoi.gov.vn/uploads/phongsuanh/thang5/30%20nam%20le%20duan/_DSC5104.JPG", "Một số hình ảnh về kỷ niệm 30 năm Ngày thành lập...", "abc", 1));
//		photoContents.add(new GalleryContent(2, "http://www.thanhdoanhanoi.gov.vn/uploads/thanhdoan/Laos2013/_DSC0757.jpg", "Hội đàm giữa Thành đoàn Hà Nội và Thành đoàn Viêng Chăn", "abc", 1));
//		photoContents.add(new GalleryContent(3, "http://www.thanhdoanhanoi.gov.vn/uploads/phongsuanh/THang3/ky%20niem%2082%20nam/_DSC6449.JPG", "Kỷ niệm 82 năm ngày thành lập Đoàn TNCS Hồ Chí Minh...", "abc", 1));
//		photoContents.add(new GalleryContent(4, "http://www.thanhdoanhanoi.gov.vn/uploads/phongsuanh/THang3/damcuoi/DSC00138.JPG", "Lễ cưới tập thể", "abc", 1));
//		photoContents.add(new GalleryContent(5, "http://www.thanhdoanhanoi.gov.vn/uploads/phongsuanh/THang3/423%20bi%20thu%20doan%20xa/5759.jpg", "Hội nghị gặp mặt 423 Bí thư đoàn xã, thị trấn", "abc", 1));
//		photoContents.add(new GalleryContent(6, "http://www.thanhdoanhanoi.gov.vn/uploads/thanhdoanHN/toadam/_DSC5517.JPG", "Tọa đàm Xây dựng phong cách cán bộ Đoàn trong thời...", "abc", 1));
//		photoContents.add(new GalleryContent(611, "http://54.243.244.187/Uploads/Events/Event_1.jpg", "Tọa đàm Xây dựng phong cách cán bộ Đoàn trong thời...", "abc", 1));
		
		btnPhoto.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				isPhotoSegment = true;
				btnPhoto.setBackgroundResource(R.drawable.button_focus_bg);
				btnPhoto.setTextColor(Color.parseColor("#ffffff"));
				btnVideo.setBackgroundResource(R.drawable.button_nonfocus_bg);
				btnVideo.setTextColor(Color.parseColor("#2388c5"));
				mAdapter = new GalleryAdapter(getApplicationContext(), photoContents, isPhotoSegment);
				gvGallery.setAdapter(mAdapter);
			}
		});
		
		btnVideo.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				isPhotoSegment = false;
				btnVideo.setBackgroundResource(R.drawable.button_focus_bg);
				btnVideo.setTextColor(Color.parseColor("#ffffff"));
				btnPhoto.setBackgroundResource(R.drawable.button_nonfocus_2);
				btnPhoto.setTextColor(Color.parseColor("#2388c5"));
				mAdapter = new GalleryAdapter(getApplicationContext(), videoContents, isPhotoSegment);
				gvGallery.setAdapter(mAdapter);
			}
		});
		
		
		gvGallery.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				if(isPhotoSegment){
					Intent intent = new Intent(getApplicationContext(), PhotoActivity.class);
					intent.putExtra("data", photoContents);
					intent.putExtra("pos", position);
					startActivity(intent);
				}
				else {
					Intent intent = new Intent(getApplicationContext(), VideoActivity.class);
					intent.putExtra("data", mAdapter.getGalleryContent(position));
					startActivity(intent);
				}
			}
		});
	}

	@Override
	protected void initProcess() {
		
		
		
	}
	
	public void onClickBack(View v){
		finish();
	}
	

}
